package edu.hitsz;

import edu.hitsz.Rank.RankListDAOMPL;
import edu.hitsz.Rank.RankObjcet;
import edu.hitsz.application.Main;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import static edu.hitsz.setName.rankListDAO;

public class Ranking {
    private JPanel mainPanel;
    private JPanel topPanel;
    public JScrollPane tableScrollPanel;
    public JTable scoreTable;
    private JPanel buttonPanel;
    private JButton deleteButton;
    private JPanel modePanel;
    private JLabel modeLabel;

    static JFrame frame = new JFrame("comfirm");

    public JPanel getMainPanel() {
        return mainPanel;
    }

    public int row;

    public static MyTableModel model = null;

    public static String[] columnName = {"排名", "用户名", "分数", "时间"};

    public Ranking(RankListDAOMPL rankListDAO) {
        ArrayList<String[]> tableData = new ArrayList();
        ArrayList<RankObjcet> ranks = rankListDAO.getAllRanks();
        for (RankObjcet r: ranks){
            String[] row = new String[4];
            row[0] = String.valueOf(r.getRank());
            row[1] = r.getUserName();
            row[2] = String.valueOf(r.getScore());
            row[3] = r.getTime();
            tableData.add(row);
        }

        String[][] tableData_array = new String[tableData.size()][4];
        tableData.toArray(tableData_array);
        model = new MyTableModel(tableData_array, columnName){
            @Override
            public boolean isCellEditable(int row, int col){
                return false;
            }
        };

        scoreTable.setModel(model);
        tableScrollPanel.setViewportView(scoreTable);
        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                row = scoreTable.getSelectedRow();
                if(row != -1){
                    frame.setContentPane(new ComFirmDelete().getMainPanel());
                    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                    frame.setBounds(((int) screenSize.getWidth() - Main.WINDOW_WIDTH) / 2 + Main.WINDOW_WIDTH/4, Main.WINDOW_HEIGHT/2, Main.WINDOW_HEIGHT/4, Main.WINDOW_WIDTH/4);
                    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    frame.setVisible(true);
                }
            }
        });
    }

    public void setModeLabel(int mode) {
        String text = null;
        if (mode == 0) {
            text = "简单模式";
        } else if (mode == 1) {
            text = "普通模式";
        } else if (mode == 2) {
            text = "困难模式";
        }
        modeLabel.setText("当前难度："+ text);
    }
}

class MyTableModel extends DefaultTableModel {
    public MyTableModel(String[][] tableDataArray, String[] columnName) {
        super(tableDataArray, columnName);
    }

    @Override
    public void removeRow(int row) {
        super.removeRow(row);
        ArrayList<String[]> tableData = new ArrayList();
        ArrayList<RankObjcet> ranks = rankListDAO.getAllRanks();
        for (RankObjcet r : ranks) {
            String[] row2 = new String[4];
            row2[0] = String.valueOf(r.getRank());
            row2[1] = r.getUserName();
            row2[2] = String.valueOf(r.getScore());
            row2[3] = r.getTime();
            tableData.add(row2);
        }
        String[][] tableData_array = new String[tableData.size()][4];
        tableData.toArray(tableData_array);
        Ranking.model = new MyTableModel(tableData_array, Ranking.columnName);
        setName.ranking.scoreTable.setModel(Ranking.model);
        setName.ranking.tableScrollPanel.setViewportView(setName.ranking.scoreTable);

    }
}